//
//  HJDAppView.m
//  KaiChat
//
//  Created by lx on 2024/3/20.
//

#import "HJDAppView.h"
#import "HJCollectionViewCell.h"
#import "QMUIButton.h"

@interface HJDAppItem: HJCollectionViewCell

@property (nonatomic, strong) NSDictionary *dict;
@property (nonatomic, weak) UIImageView *avatarImageView;
@property (nonatomic, weak) UIImageView *flagImageView;
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, copy) void (^didClickDeleteButtonBlock)(void);
@property (nonatomic, weak) UILabel *flagLabel;


@end

@implementation HJDAppItem


- (void)settingSubView {
    [super settingSubView];

    UIImageView *avatarImageView = [UIImageView addToView:self.contentView];
    avatarImageView.addCornerRadius(20);
    avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(6);
        make.centerX.offset(0);
        make.width.height.offset(40);
    }];
 
    UILabel *nameLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMake(12) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentCenter];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(avatarImageView.mas_bottom).offset(6);
    }];
    
    UIImageView *flagImageView = [UIImageView addToView:self.contentView];
    UIImage *bubbleImage = [UIImage imageNamed:@"dapp_tag"];
    UIEdgeInsets insets = UIEdgeInsetsMake(9, 12, 9, 12);
    UIImage *resizableBubbleImage = [bubbleImage resizableImageWithCapInsets:insets];
    flagImageView.image = resizableBubbleImage;
    [flagImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(avatarImageView.mas_centerX);
        make.bottom.equalTo(avatarImageView.mas_top).offset(10);
        make.width.offset(25);
        make.height.offset(18);
    }];
    
    UILabel *flagLabel = [UILabel lableWithSuperView:flagImageView font:UIFontMake(10) color:UIColor.whiteColor title:nil textAlignment:NSTextAlignmentCenter];
    [flagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    flagImageView.hidden = YES;
    _flagImageView = flagImageView;
    _avatarImageView = avatarImageView;
    _nameLabel = nameLabel;
    _flagLabel = flagLabel;
}

- (void)setDict:(NSDictionary *)dict {
    _dict = dict;
    
    if (isEmptyString(dict[@"statement"])) {
        [self.flagImageView mas_remakeConstraints:^(MASConstraintMaker *make) { }];
        _flagImageView.hidden = YES;
        _flagLabel.text = nil;
    } else {
        _flagImageView.hidden = NO;
        _flagLabel.text = dict[@"statement"];
        CGFloat flagW = [_flagLabel.text getStringSizeWithWidth:100 font:_flagLabel.font].width + 10;
        [self.flagImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.avatarImageView.mas_centerX);
            make.bottom.equalTo(self.avatarImageView.mas_top).offset(10);
            make.width.offset(flagW);
            make.height.offset(18);
        }];
    }
    
    _nameLabel.text = dict[@"name"];
    [_avatarImageView sd_setImageWithURL:[dict[@"logo"] URL] placeholderImage:[UIImage imageNamed:defaultAvatarName]];
}
 
- (void)didClickDeleteButton {
    if (_didClickDeleteButtonBlock) {
        _didClickDeleteButtonBlock();
    }
}

@end

@interface HJDAppView()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

@end


@implementation HJDAppView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        
        UIView *topView = [UIView addToView:self];
        [topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.offset(0);
            make.height.offset(HZoom(40));
        }];
        
        UILabel *titleLabel = [UILabel lableWithSuperView:topView font:UIFontMediumMake(16) color:UIColor.tj_333333Color title:@"Suggest" textAlignment:NSTextAlignmentLeft];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(16);
            make.left.offset(leftRightMargin);
        }];
        
        QMUIButton *moreButton = [QMUIButton buttonWithSuperView:topView font:UIFontMake(14) color:UIColor.tj_6A6A6AColor title:@"More"];
        [moreButton setImage:[UIImage imageNamed:@"cell_arrow"] forState:UIControlStateNormal];
        [moreButton setImagePosition:QMUIButtonImagePositionRight];
        [moreButton setSpacingBetweenImageAndTitle:2];
        [moreButton sizeToFit];
        [moreButton addTarget:self action:@selector(didClickMoreButton) forControlEvents:UIControlEventTouchUpInside];
        CGSize moreButtonSize = moreButton.tj_size;
        [moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.inset(leftRightMargin);
            make.centerY.offset(0);
            make.width.offset(moreButtonSize.width);
            make.height.offset(32);
        }];
        
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(topView.mas_bottom);
            make.left.right.bottom.offset(0);
        }];
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return MIN(self.datas.count, 4);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HJDAppItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HJDAppItem" forIndexPath:indexPath];
    cell.dict = self.datas[indexPath.row];;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.datas[indexPath.row];
    if (self.didSelectItemAtIndexPathBlock) {
        self.didSelectItemAtIndexPathBlock(dict);
    }
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _flowLayout = flowLayout;
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
        flowLayout.itemSize = CGSizeMake((KScreenWidth - 15 * 2 - leftRightMargin * 2) / 4, HZoom(84));
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:HJDAppItem.class forCellWithReuseIdentifier:@"HJDAppItem"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.bounces = NO;
        _collectionView.scrollEnabled = NO;
        
    }
    return _collectionView;
}

- (void)didClickMoreButton {
    if (_didClickMoreBlock) {
        _didClickMoreBlock();
    }
}


- (void)setDatas:(NSArray *)datas {
    _datas = datas;
    
    [self reloadData];
}

- (void)reloadData {
    [self.collectionView reloadData];
}

- (void)dealloc {
    TJLog(@"%s", __func__);
}

@end
